//
//  MCTicketCell.swift
//  WisdomSpace
//
//  Created by goulela on 2017/9/4.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

protocol MCTicketCellDelegate : NSObjectProtocol {
    func MCTicketCellPushto(viewController:UIViewController)
    func MCTicketCellDelete(row:Int)
}

class MCTicketCell: UITableViewCell {
    
    var row = 0
    
    private let bgImageWidth : CGFloat = (MCScreenWidth-30)/2.65
    private let bgImageHeigh : CGFloat = (MCScreenWidth-30)*3/7 + 15
    
    weak var delegate : MCTicketCellDelegate?

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.MCGray_light
        initUI()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()

    }
    
    
    var model : MCTicketModel = MCTicketModel() {
        
        didSet {
            var moneyOrDiscountStr : String
            
            if (model.isDiscount == 0) {
                let moneyStr = MCString_judgemantDecimalsIsZero(figure: model.amount.intValue)
                
                moneyOrDiscountStr = moneyStr.MCString_prefixFromeBeginIndex(1, index: moneyStr.count - 1) + "元"
                
                limitLabel.text = "满" + MCString_judgemantDecimalsIsZero(figure: model.limited.intValue)  + "使用"
            } else {
                let discount : CGFloat = CGFloat((100 - self.model.discount.floatValue) / 10.0)
                moneyOrDiscountStr = String.init(format: "%.1f折", discount)
                limitLabel.text = "满\(model.limited.intValue/100)元使用"
            }
            
            moneyLabel.attributedText = moneyOrDiscountStr.MCAttributedString_font(font: 18, range: NSRange.init(location: moneyOrDiscountStr.count - 1, length: 1))

            
            
            switch model.type.intValue {
            case 2:    // 通用券
                categoryImageView.image = #imageLiteral(resourceName: "Ticket_normal")
            case 3:    // 品牌券
                categoryImageView.image = #imageLiteral(resourceName: "Ticket_brand")
            case 5:    // 商品券
                categoryImageView.image = #imageLiteral(resourceName: "Ticket_goods")
            default:
                break
            }


            timeLabel.text = "生效期: " +  model.effectiveTime.MCString_prefix(index: 16) + "\n" + "截止期: " + model.expirationTime.MCString_prefix(index: 16)
            marketShowLabel.isHidden = !model.forMallDisplay.boolValue

            titleLabel.text = model.promotionDetailName


            residueLabel.text = "剩余:" + model.remainingNumber.stringValue + "张"
            useLabel.text = "已使用:" + String(model.totalNumber.intValue - model.remainingNumber.intValue) + "张"

        }
    }
    
    
    @objc func deleteButtonClicked() {
        delegate?.MCTicketCellDelete(row: row)
    }
    
    @objc func editButtonClicked() {
        let vc = MCAddTicketViewController()
        vc.isUpdate = true
        vc.promotionDetailId = model.promotionDetailId.intValue
        delegate?.MCTicketCellPushto(viewController: vc)
    }

    @objc func sendButtonClicked() {
        let vc = MCShareTicketViewController()
        vc.model = self.model
        delegate?.MCTicketCellPushto(viewController: vc)
    }

    
    func initUI() {
        
        self.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(self).inset(UIEdgeInsetsMake(15, 15, 0, 15))
        }
        
        bgView.addSubview(cropView)
        cropView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(bgView)
        }

        
        cropView.addSubview(bgImageView)
        bgImageView.snp.remakeConstraints { (make) -> Void in
            make.left.bottom.top.equalTo(bgView).offset(0)
            make.width.equalTo(bgImageWidth)
        }
        
        let gradientColors = [MCColor_16(colorStr: "6BB9F6"),
                              MCColor_16(colorStr: "83D8FF")]
        
        bgImageView.image = UIImage.init(color: UIColor.white)?.apply(gradientColors: gradientColors)

        
        cropView.addSubview(categoryImageView)
        categoryImageView.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-15)
            make.top.equalTo(0)
            make.size.equalTo(CGSize.init(width: 24, height: 58))
        }
        


        
        bgImageView.addSubview(moneyLabel)
        moneyLabel.snp.remakeConstraints { (make) -> Void in
            make.centerX.equalTo(bgImageView)
            make.bottom.equalTo(bgImageView.snp.centerY).offset(-3)
        }
        
        bgImageView.addSubview(limitLabel)
        limitLabel.snp.remakeConstraints { (make) -> Void in
            make.centerX.equalTo(bgImageView)
            make.top.equalTo(bgImageView.snp.centerY).offset(5)
        }
        

        
        cropView.addSubview(titleLabel)
        titleLabel.snp.remakeConstraints { (make) -> Void in
            make.left.equalTo(bgImageView.snp.right).offset(10)
            make.right.equalTo(categoryImageView.snp.left).offset(-5)
            make.top.equalTo(cropView).offset(10)
        }
        
        cropView.addSubview(timeLabel)
        timeLabel.snp.remakeConstraints { (make) -> Void in
            make.left.equalTo(bgImageView.snp.right).offset(10)
            make.right.equalTo(categoryImageView.snp.left).offset(-5)
            make.top.equalTo(titleLabel.snp.bottom).offset(5)
        }

        cropView.addSubview(marketShowLabel)
        marketShowLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(titleLabel)
            make.size.equalTo(CGSize.init(width: 85, height: 16))
            make.top.equalTo(timeLabel.snp.bottom).offset(5)
        }




        
        
        let size = CGSize.init(width: 50, height: 22)
        
        let margin = (MCScreenWidth - 30 - bgImageWidth - 20 - size.width * 3) / 2
        
        cropView.addSubview(editButton)
        editButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(titleLabel)
            make.bottom.equalTo(bgImageView.snp.bottom).offset(-10)
            make.size.equalTo(size)
        }

        
        cropView.addSubview(deleteButton)
        deleteButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(editButton.snp.right).offset(margin)
            make.centerY.equalTo(editButton)
            make.size.equalTo(size)
        }

        cropView.addSubview(sendButton)
        sendButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(cropView).offset(-15)
            make.centerY.equalTo(editButton)
            make.size.equalTo(size)
        }

        
        cropView.addSubview(residueLabel)
        residueLabel.snp.remakeConstraints { (make) -> Void in
            make.left.equalTo(titleLabel)
            make.bottom.equalTo(editButton.snp.top).offset(-5)
            make.height.equalTo(15)
        }
        
        cropView.addSubview(useLabel)
        useLabel.snp.remakeConstraints { (make) -> Void in
            make.left.equalTo(residueLabel.snp.right).offset(25)
            make.centerY.equalTo(residueLabel)
        }
        
        
        // 只有店长才可编辑，修改，删除，新增
        if MCUserinfo_getLevel() >= MCRole_manager {
            editButton.isHidden = false
            deleteButton.isHidden = false
        } else {
            editButton.isHidden = true
            deleteButton.isHidden = true
        }
    }
    
    private lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.clear
        view.layer.shadowColor = UIColor.MCGray_middle.cgColor
        view.layer.shadowOffset = CGSize.init(width: 0, height: 0)
        view.layer.shadowOpacity = 0.5
        view.layer.shadowRadius = 5
        return view
    }()
    
    private lazy var cropView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 5
//        view.layer.borderColor = UIColor.MCGray_light.cgColor
//        view.layer.borderWidth = 1
        return view
    }()

    
    private lazy var bgImageView: UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()
    
    
    private lazy var categoryImageView: UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()
    
    private lazy var moneyLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(35)
        label.textColor = UIColor.white
        return label
    }()

    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 16)
        label.textColor = UIColor.MCWords
        return label
    }()
    
    private lazy var limitLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.textColor = UIColor.white
        return label
    }()
    
    
    private lazy var marketShowLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(11)
        label.layer.cornerRadius = 2
        label.layer.masksToBounds = true
        label.layer.borderColor = UIColor.MCMain.cgColor
        label.layer.borderWidth = 1
        label.text = "在微商城中显示"
        label.textAlignment = NSTextAlignment.center
        label.textColor = UIColor.MCMain
        return label
    }()


    private lazy var residueLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        label.text = "剩余:0"
        return label
    }()

    
    private lazy var useLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        label.text = "使用:0"
        return label
    }()

    
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        label.numberOfLines = 0
        return label
    }()
    

    
    lazy var editButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
        button.titleLabel?.font = MCFont(12)
        button.setImage(#imageLiteral(resourceName: "Mine_edit").MCImageWithTintColor(color: UIColor.MCGray_middle), for: .normal)
        button.setTitle(" 编辑", for: UIControlState.normal)
        button.setTitleColor(UIColor.MCGray_middle, for: UIControlState.normal)
        button.addTarget(self, action: #selector(editButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var deleteButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(12)
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left

        button.setImage(#imageLiteral(resourceName: "Mine_edit").MCImageWithTintColor(color: UIColor.MCGray_middle), for: .normal)
        button.setTitle(" 删除", for: UIControlState.normal)
        button.setTitleColor(UIColor.MCGray_middle, for: UIControlState.normal)
        button.addTarget(self, action: #selector(deleteButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var sendButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)

        button.titleLabel?.font = MCFont(12)
        button.layer.cornerRadius = 11
        button.layer.masksToBounds = true
        button.layer.borderColor = MCColor_16(colorStr: "2E99FF").cgColor
        button.layer.borderWidth = 1
        button.setTitleColor(MCColor_16(colorStr: "2E99FF"), for: .normal)
        button.setTitle("发券", for: UIControlState.normal)
        button.addTarget(self, action: #selector(sendButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
}
